package com.aaa.dang.modules.system.service.impl;

import com.aaa.dang.common.entity.OperationLog;
import com.aaa.dang.common.util.ResultModel;
import com.aaa.dang.modules.system.dao.IOperationLogDao;
import com.aaa.dang.modules.system.service.IOperationLogService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

import static com.aaa.dang.common.systemconst.ISystemConst.SUCCESS_CODE;

/**
 * 系统日志业务实现类
 *
 * @author Dadazhao
 * @date 2018/12/26
 */
@Service
public class OperationLogServiceImpl implements IOperationLogService {

    /**
     * 注入日志业务层
     */
    @Autowired
    private IOperationLogDao operationLogDao;
    @Autowired
    public ResultModel model;

    /**
     * 添加一条日志
     *
     * @param operationLog 日志实体
     * @return 影响行数
     */
    @Override
    public int addOperationLog(OperationLog operationLog) {
        return operationLogDao.addOperationLog(operationLog);
    }

    /**
     * 获得部分日志记录
     *
     * @param query 查询条件
     * @return 日志列表
     */
    @Override
    public ResultModel getOperationLogLimit(Map<String, Object> query) {
        model.setCode(SUCCESS_CODE);
        //查询到的日志信息
        model.setData(operationLogDao.getOperationLogLimit(query));
        model.setCount(0);
        return model;
    }

    /**
     * 查询日志信息
     *
     * @param query 查询条件
     * @return 日志列表
     */
    @Override
    public ResultModel getOperationLogAll(Map<String, Object> query) {
        //当page  和 limit 的值不为null或"" 时 进行分页 否则查询所有
        Boolean isPaging = (query.get("page") != null && query.get("page") != "") && ((query.get("limit") != null && query.get("limit") != ""));
        if (isPaging) {
            //一页显示多少行
            int pageSize = Integer.parseInt(query.get("limit") + "");
            //当前第几页
            int pageNo = Integer.parseInt(query.get("page") + "");
            //分页插件
            PageHelper.startPage(pageNo, pageSize);
        }
        model.setCode(SUCCESS_CODE);
        model.setMsg("加载成功");
        model.setData(operationLogDao.getOperationLogAll(query));
        model.setCount(operationLogDao.getOperationLogTotalAll(query));
        return model;
    }
}
